package com.holyn.guofang.view.distinguish;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.bumptech.glide.Glide;
import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.BrandInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.distinguish.SearchResultDetailContract;
import com.holyn.guofang.presenter.distinguish.SearchResultDetailPresenter;
import com.holyn.guofang.view.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/7/19.
 */

public class SearchResultDetailActvity extends BaseActivity implements BaseView, SearchResultDetailContract.DetailView{
    private static final int REQUEST_ACTION_POST_DETAIL = 0x0001;

    public static final String INTENT_KEY_MARK_NO = "INTENT_KEY_MARK_NO";
    public static final String INTENT_KEY_MARK_CLASS = "INTENT_KEY_MARK_CLASS";

    @BindView(R.id.tv_look_notice)
    TextView tvLookNotice;
    @BindView(R.id.tv_look_process)
    TextView tvLookProcess;
    @BindView(R.id.tv_look_service)
    TextView tvLookService;
    @BindView(R.id.iv_MarkImage)
    ImageView ivMarkImage;
    @BindView(R.id.tv_MarkNo)
    TextView tvMarkNo;
    @BindView(R.id.tv_MarkClass)
    TextView tvMarkClass;
    @BindView(R.id.tv_MarkType)
    TextView tvMarkType;
    @BindView(R.id.tv_MarkShowName)
    TextView tvMarkShowName;
    @BindView(R.id.tv_ApplicationDate)
    TextView tvApplicationDate;
    @BindView(R.id.tv_RegisterPublishDate)
    TextView tvRegisterPublishDate;
    @BindView(R.id.tv_HolderNameCn)
    TextView tvHolderNameCn;
    @BindView(R.id.tv_HolderAddrCn)
    TextView tvHolderAddrCn;
    @BindView(R.id.tv_StartDate_EndDate)
    TextView tvStartDateEndDate;
    @BindView(R.id.tv_AgentName)
    TextView tvAgentName;
    @BindView(R.id.tv_MarkGroup)
    TextView tvMarkGroup;
    @BindView(R.id.tv_Goods)
    TextView tvGoods;
    @BindView(R.id.tv_IsValid)
    TextView tvIsValid;
    @BindView(R.id.tv_LastPublishName)
    TextView tvLastPublishName;
    @BindView(R.id.tv_LastProcessName)
    TextView tvLastProcessName;

    SearchResultDetailContract.Presenter presenter;

    String MarkNo;
    Integer MarkClass;

    public static void start(Activity activity, String MarkNo, Integer MarkClass){
        Intent intent = new Intent(activity, SearchResultDetailActvity.class);
        intent.putExtra(INTENT_KEY_MARK_NO, MarkNo);
        intent.putExtra(INTENT_KEY_MARK_CLASS, MarkClass);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_search_result_detail;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("商标档案");

        MarkNo = getIntent().getStringExtra(INTENT_KEY_MARK_NO);
        MarkClass = getIntent().getIntExtra(INTENT_KEY_MARK_CLASS, 0);

        presenter = new SearchResultDetailPresenter(this);
        presenter.addDetailView(this);

        presenter.postTmInfbyMarkNo(REQUEST_ACTION_POST_DETAIL, MarkNo, MarkClass);

    }

    @OnClick({R.id.tv_look_notice, R.id.tv_look_process, R.id.tv_look_service})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_look_notice:
                ProductDetailNoticesAvtivity.start(SearchResultDetailActvity.this, MarkNo, MarkClass);
                break;
            case R.id.tv_look_process:
                ProductDetailProcessAvtivity.start(SearchResultDetailActvity.this, MarkNo, MarkClass);
                break;
            case R.id.tv_look_service:
                ProductDetailServiceAvtivity.start(SearchResultDetailActvity.this, MarkNo, MarkClass);
                break;
        }
    }

    @Override
    public void requestStart(Integer requestAction) {
        LogUtils.d("requestStart....");
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        LogUtils.d("requestError...."+e);
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        LogUtils.d("requestComplete....");
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(Integer requestAction, BrandInfoWo brandInfoWo) {
        LogUtils.d(brandInfoWo);
        if (brandInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            BrandInfoWo.DataBean.MarkInfoBean markInfoBean = brandInfoWo.getData().getMarkInfo().get(0);
            if (markInfoBean == null){
                toastMsg("返回数据为空！");
            } else {
                Glide.with(SearchResultDetailActvity.this).load(markInfoBean.getMarkImagePath())
//                        .error(R.drawable.ic_empty_image)
                        .into(ivMarkImage);
                tvMarkNo.setText(markInfoBean.getMarkNo());
                tvMarkClass.setText(markInfoBean.getMarkClass()+"");
                tvMarkType.setText(markInfoBean.getMarkType());
                tvMarkShowName.setText(markInfoBean.getMarkShowName());
                tvApplicationDate.setText(markInfoBean.getApplicationDate());
                tvRegisterPublishDate.setText(markInfoBean.getRegisterPublishDate());
                tvHolderNameCn.setText(markInfoBean.getHolderNameCn());
                tvHolderAddrCn.setText(markInfoBean.getHolderAddrCn());
                if (TextUtils.isEmpty(markInfoBean.getStartDate()) || TextUtils.isEmpty(markInfoBean.getEndDate())){
                    tvStartDateEndDate.setText("");
                } else {
                    tvStartDateEndDate.setText(markInfoBean.getStartDate()+" 至 "+markInfoBean.getEndDate());
                }
                tvAgentName.setText(markInfoBean.getAgentName());
                tvMarkGroup.setText(markInfoBean.getMarkGroup());
                tvGoods.setText(markInfoBean.getGoods());
                String isValid = markInfoBean.getIsValid();
                tvIsValid.setText(isValid);
                tvLastPublishName.setText(markInfoBean.getLastPublishName());
                tvLastProcessName.setText(markInfoBean.getLastProcessName());
            }
        } else {
            if (TextUtils.isEmpty(brandInfoWo.getErr())){
                toastMsg("获取数据失败！");
            } else {
                toastMsg(brandInfoWo.getErr());
            }
        }
    }
}
